package com.xiaoma.verbal.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xiaoma.verbal.baseDao.BaseDaoImpl;
import com.xiaoma.verbal.baseDao.DefaultRowMapper;
import com.xiaoma.tpo.domain.ChChapter;
import com.xiaoma.tpo.utils.Pager;
import com.xiaoma.verbal.domain.FctForecast;
import com.xiaoma.verbal.domain.FctQuestion;
import com.xiaoma.verbal.domain.QuQuestion;
import com.xiaoma.verbal.dto.QuestionVo;

@Repository
public class QuQuestionDao extends BaseDaoImpl<QuQuestion> {
	private static final Logger LOG = LoggerFactory.getLogger(QuQuestionDao.class);

	public Pager queryPageList(Integer id,Integer questionType,String qnum, String tagText,String content,  Integer startIndex, Integer itemsPerPage) {
		Pager pager = new Pager();
		List<Object> params = new ArrayList<Object>();
		String sqlQuery = "";
		String sqlCount = "";
		sqlQuery = "select a.* ";
		String sqlTrunk = " from qu_question a left join qu_tag b on a.id = b.question_id left join  tag_table  c on b.tag_id = c.id ";
		String where = " where 1=1 ";
		String orderBy = " order by a.id desc ";
		String limit = " limit ?,? ";
		String groupBy = " group by a.id ";
		if (id != null && !"".equals(id)) {
			where = where + " and a.id = ? ";
			params.add(id);
		}
		if (qnum != null && !"".equals(qnum)) {
			where = where + " and a.question_num = upper(?) ";
			params.add(qnum.toUpperCase());
		}
		if (content != null && !"".equals(content)) {
			where = where + " and a.content like ? ";
			params.add("%"+content+"%");
		}
		if (questionType != null && !"".equals(questionType)) {
			where = where + " and a.question_type = ? ";
			params.add(questionType);
		}
		if (tagText != null && !"".equals(tagText)) {
			where = where + " and c.tag_text like ? ";
			params.add("%"+tagText+"%");
		}
	
		
		sqlQuery = sqlQuery + sqlTrunk + where + groupBy + orderBy + limit;
		sqlCount = "select count(*)  from (select a.*  " +sqlTrunk + where + groupBy +" ) as a";
		LOG.info(sqlQuery + " PARAMS: " + startIndex + ", " + itemsPerPage);
		LOG.info(sqlCount);
		int totalCount = jdbcTemplate.queryForInt(sqlCount,params.toArray());
		
		params.add(startIndex);
		params.add(itemsPerPage);
		List<?> list = jdbcTemplate.query(sqlQuery, params.toArray(), new DefaultRowMapper(QuestionVo.class, this.getNameHandler()));
	
		pager.setList(list);
		pager.setTotalCount(totalCount);
		return pager;
	}

	public QuQuestion findByQnum(String qNum) {
		// TODO Auto-generated method stub
		String sqlQuery = "select * from qu_question where question_num = ? limit 1";
		List<?> list = jdbcTemplate.query(sqlQuery, new DefaultRowMapper(QuQuestion.class, this.getNameHandler()),qNum);
		if(list!=null && list.size()>0){
			return (QuQuestion) list.get(0);
		}
		return null;
	}
}
